
package com.android.formula.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;

import com.android.formula.R;
import com.android.formula.common.DialogsManager;
import com.android.formula.utils.AppUtils;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public abstract class BaseActivity extends Activity implements OnClickListener {
    private DialogsManager mDialogsManager;

    protected AdView mAdView;

    protected AdListener mAdListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initAds();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadDataScreen();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(getLayoutInflater().inflate(layoutResID, null));
    }

    // Dialogs Manager ----------------------------------->
    protected final DialogsManager getDialogsManager() {
        if (mDialogsManager == null) {
            mDialogsManager = new DialogsManager(this);
        }
        return mDialogsManager;
    }

    protected void releaseObjects() {
        mDialogsManager = null;
    }

    public final void setAutoHideKeyboard(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    protected void optimizeMemory() {
        System.gc();
        System.runFinalization();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // Handle onClick() ----------------------------------->
    @Override
    public void onClick(View view) {
        if (view == null) {
            return;
        }
        synchronized (view) {
            if (!AppUtils.startClickView(view)) {
                return;
            }
            onClickWithDelay(view);
        }

    }

    protected void onClickWithDelay(View view) {

    }

    protected void initAds() {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(mAdListener);
    }
    
    protected void setAdListener(){
        mAdListener = new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                super.onAdFailedToLoad(errorCode);
                mAdView.setVisibility(View.GONE);
            }
            
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mAdView.setVisibility(View.VISIBLE);
            }
        };
    }

    public abstract void initView();

    public abstract void loadDataScreen();
}
